#import "HLayoutView.h"

@implementation HLayoutView

- (id)initWithSpacing:(int)s
{
    if (self = [super init])
    {
        spacing = s;
    }
    return self;
}

- (CGSize)layoutSubviewsEffectively:(BOOL)effectively
{
    int x = 0, max_y = 0;
    for (UIView *child in self.subviews)
    {
        if (x != 0)
            x += spacing;
        if (effectively)
            child.frame = CGRectMake(x, 0, child.frame.size.width,
                                     child.frame.size.height);
        x += child.frame.size.width;
        if (max_y < child.frame.size.height)
            max_y = child.frame.size.height;
    }
    return CGSizeMake(x, max_y);
}

- (void)layoutSubviews
{
    [self layoutSubviewsEffectively:YES];
}

- (CGSize)sizeThatFits:(CGSize)size
{
    return [self layoutSubviewsEffectively:NO];
}

@end
